//noinspection TypeScriptCheckImport
import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { RouterModule, Routes, Router } from '@angular/router';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
    // data
    // 菜单列表
    menuList = [
        {
            id: 1,
            router: '/table',
            titleName: '表格',
            selected: true
        },
        {
            id: 2,
            router: '/form',
            titleName: '表单',
            selected: false
        },
        {
            id: 3,
            router: '/layers',
            titleName: '层级页面',
            selected: false
        },
        /*

         {
         id: 4,
         router: '/test',
         titleName: '表格',
         selected: false
         },
         {
         id: 5,
         router: '/test',
         titleName: '表格',
         selected: false
         },
         {
         id: 6,
         router: '/test',
         titleName: '表格',
         selected: false
         },
         {
         id: 7,
         router: '/test',
         titleName: '表格',
         selected: false
         }*/
    ];

    user = {
        name: '',
        password: ''
    };
    path = '';

    clickMenu(selectedMenu) {
        let vm = this;
        for (let i = 0; i < vm.menuList.length; i++) {
            vm.menuList[i].selected = false;
        }
        selectedMenu.selected = true;
    }

    // 是否显示公共组件(顶部和左侧菜单栏)
    isShowPublicComponent = true;

    goToLogin() {
        let vm = this;
        vm.isShowPublicComponent = false;
    }

    constructor(public Location:Location, public router:Router) {
        console.log(router);
    }

    ngOnInit() {
        let vm = this;
        // 校验用户是否登录,否则跳转登录页面
        let user = JSON.parse(localStorage.getItem('user'));
        if (!user || !user.name || !user.password) {
            vm.router.navigate(['/login']);
        } else {
            vm.user = user;
        }

        vm.router.events
            .subscribe((event) => {
                // 登录后如果不刷新,不出现用户名
                vm.user = JSON.parse(localStorage.getItem('user'));
                // todo 判断当前页面,控制左侧栏和顶部栏的显示,如果是登陆注册页则不显示
                let path = vm.Location.path();
                if (path == '/login' || path == '/register') {
                    vm.isShowPublicComponent = false;
                } else {
                    vm.isShowPublicComponent = true;
                }
                // 判断当前菜单
                vm.path = vm.Location.path();
                for (let i = 0; i < vm.menuList.length; i++) {
                    if (vm.menuList[i].router == vm.path) {
                        vm.menuList[i].selected = true;
                    } else {
                        vm.menuList[i].selected = false;
                    }
                }
                // todo 会执行很多次,目前不清楚路由监测原理,后期需要改进
            });
    }

}
